import edu.princeton.cs.algs4.*;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedBag<Item> implements Iterable<Item>
{
    private Node first;
    private int n;

    private class Node
    {
        private Item item;
        private Node next;
    }
    
    public LinkedBag()
    {
        first = null;
        n = 0;
    }
    public boolean isEmpty()
    {
        return first == null;
    }

    public int size()
    {
        return n;
    }

    public void add(Item item)
    {
        Node oldFirst = first;
        first = new Node();
        first.item = item;
        first.next = oldFirst;
        n++;
    }

    public Iterator<Item> iterator()
    {
        return new LinkedIterator();
    }

    private class LinkedIterator implements Iterator<Item>
    {
        private Node current;

        public LinkedIterator()
        {
            current = first;
        }

        public boolean hasNext()
        {
            return current != null;
        }

        public Item next()
        {
            if(!hasNext())
                throw new NoSuchElementException();

            Item item = current.item;
            current = current.next;
            return item;
        }
    }

    public static void main(String[] args)
    {
        LinkedBag<String> bag = new LinkedBag<String>();
        int n = 20;
        while(true)
        {
            String item = StdIn.readString();
            bag.add(item);
            if(n-- == 0)
                break;
        }

        StdOut.println("size of bag = " + bag.size());
        for(String s : bag)
            StdOut.println(s);
    }

}
